package com.zone.infrastructure.event;

import com.alibaba.fastjson.JSONObject;
import com.zone.types.event.BaseMessage;
import com.zone.types.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zongzi
 * @description: rabbitmq消息生产者
 * @date: 2025/2/16
 */
@Slf4j
@Service
public class RabbitProduct implements MessageProduct {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public <T> void sendMessage(String topic, T data) {
        log.info("send message start, message info ====> {}", JSONObject.toJSONString(data));
        if (Strings.isBlank(topic)) {
            throw new BizException("send message error topic is empty");
        }
        BaseMessage<T> baseMessage = new BaseMessage<>(data);
        rabbitTemplate.convertAndSend(topic, baseMessage);
        log.info("send message success");
    }
}
